package cn.yc.file;

import org.junit.jupiter.api.Test;

import java.io.File;

public class Directory_ {
    public static void main(String[] args) {
        // 删除文件
        String filePath = "e:\\io\\new1.txt";
        File file = new File(filePath);
        if (file.exists()) {
            boolean delete = file.delete();
            if (delete) {
                System.out.println("删除成功");
            } else {
                System.out.println("删除失败");
            }
        }
    }

    // 创建目录，要体会java中目录也被当做文件
    @Test
    public void createDirectory() {
        String filePath = "e:\\io\\newDirectory";
        File file = new File(filePath);
        boolean mkdir = file.mkdir();
        if (mkdir) {
            System.out.println("创建成功");
        } else {
            System.out.println("创建失败");
        }
    }

    // 删除目录
    @Test
    public void deleteDirectory() {
        String filePath = "e:\\io\\newDirectory";
        File file = new File(filePath);
        if (file.exists()) {
            boolean delete = file.delete();
            if (delete) {
                System.out.println("删除成功");
            } else {
                System.out.println("删除失败");
            }
        }
    }

    // 创建多个目录
    @Test
    public void createDirectories() {
        String filePath = "e:\\io\\newDirectory\\newDirectory1\\newDirectory2";
        File file = new File(filePath);
        if (!file.exists()) {
            boolean mkdirs = file.mkdirs();
            if (mkdirs) {
                System.out.println("创建成功");
            } else {
                System.out.println("创建失败");
            }
        }else {
            System.out.println(filePath+"目录已存在");
        }
    }
}
